﻿using System;
using System.Drawing;
using System.IO;

namespace BingDesktopChanger
{
    internal class FileImageManager : IImageManager
    {

        public FileImageManager()
        {
            //this.filesPrefix = this.GetFilePrefix(DateTime.Today);
            this.ReadFolder();
        }

        private string imageFolder;

        public string ImageFolder
        {
            get { return this.imageFolder; }
        }
        public string GenerateImageFullPath(string imageName)
        {
            return Path.Combine(this.ImageFolder, string.Concat(Path.GetFileNameWithoutExtension(imageName), ".jpg"));
        }
        private void ReadFolder()
        {
            string folder = CommonConfig.Default.ImagesFolder;
            string realFolder = Path.IsPathRooted(folder)
                ? folder
                : Path.Combine(Environment.CurrentDirectory, folder);

            if (!Directory.Exists(realFolder))
                Directory.CreateDirectory(realFolder);

            this.imageFolder = realFolder;
        }

        #region IImageManager Members
        public void DeleteImage(string imageName)
        {
            string fullFilePath = this.GenerateImageFullPath(imageName);
            if (File.Exists(fullFilePath))
                File.Delete(fullFilePath);
        }
        public void DeleteImages(DateTime targetDay)
        {
            throw new NotImplementedException();
        }
        public void DeleteImages(DateTime fromDate, DateTime toDate)
        {
            throw new NotImplementedException();
        }
        public bool ExistsDayImages(DateTime targetDate)
        {
            throw new NotImplementedException();
        }
        public bool ExistsImage(string imageName)
        {
            return File.Exists(this.GenerateImageFullPath(imageName));
        }
        public string[] GetImageList(DateTime targetDate)
        {
            throw new NotImplementedException();
        }
        public void SaveImage(Stream imageContent, DateTime targetDay, string imageName)
        {
            if (imageContent != null)
                Image.FromStream(imageContent).Save(this.GenerateImageFullPath(imageName));
        }
        #endregion

    }
}
